// pages/admin/admin.js
var app=getApp()
Page({

  data: {
    isSelect1:false,//展示类型？
    types:['华港区','凯旋区'],
    type:"",
    isSelect2:false,//展示类型？
    types1:['待审核','待过户','待抵押','待放款','已放款','审核拒绝'],
    type1:"",
    isSelect3:false,
    type3:"",
    date:"",
    username:'',
    date1:""
  },
  del:function(e){
    var that=this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.url+'/client/delete',
            method:"POST",
            header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
            "token":app.globalData.token},
            data:{
              clientId:e.currentTarget.dataset.id
            },
            success(res){
              that.details()
            }
          })
          } else if (sm.cancel) {
            console.log('用户点击取消')
          }
        }
})
  },
  goUpdate:function(event){
    wx.navigateTo({
      url: '/pages/update/update',
    })
    console.log(event.currentTarget)
    wx.setStorage({
        key:'which',
        data:event.currentTarget.dataset.id
    })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindDateChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date1: e.detail.value
    })
  },
    //点击控制下拉框的展示、隐藏
    select:function(){
      var isSelect1 = this.data.isSelect1
      this.setData({ isSelect1:!isSelect1})
    },
    select3:function(){
      var isSelect3 = this.data.isSelect3
      this.setData({ isSelect3:!isSelect3})
    },
    //点击下拉框选项，选中并隐藏下拉框
    getType:function(e){
      let value = e.currentTarget.dataset.type
      this.setData({
        type:value ,
        isSelect1: false,
      })
    },
    getType3:function(e){
      let value = e.currentTarget.dataset.type3
      this.setData({
        type3:value ,
        isSelect3: false,
      })
    },
    salesman:function(){
      var that=this
      if(app.globalData.role==3){
        wx.request({
          url:  app.globalData.url+'/client/listSalesman1',
          method:'POST',
          header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
          "token":app.globalData.token,
        },
          success(res){
            console.log(res)
            that.setData({
              types3:res.data.data
            })
          }
        })
      }
      else if(app.globalData.role==4){
        wx.request({
          url:  app.globalData.url+'/client/listSalesman2',
          method:"POST",
          header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
          "token":app.globalData.token,
        },
          success(res){
            console.log(res)
            that.setData({
              types3:res.data.data
            })
          }
        })
      }
    },
    select1:function(){
      var isSelect2 = this.data.isSelect2
      this.setData({ isSelect2:!isSelect2})
    },
    //点击下拉框选项，选中并隐藏下拉框
    getType1:function(e){
      let value = e.currentTarget.dataset.type
      this.setData({
        type1:value ,
        isSelect2: false,
      })
    },
    search:function(){
      var that=this
      var num=0
      if(that.data.type1=="待审核"){
        that.data.type1=1
      }
      if(that.data.type1=="待过户"){
        that.data.type1=2
      }
      if(that.data.type1=="待抵押"){
        that.data.type1=3
      }
      if(that.data.type1=="待放款"){
        that.data.type1=4
      }
      if(that.data.type1=="已放款"){
        that.data.type1=5
      }
      if(that.data.type1=="审核拒绝"){
        that.data.type1=6
      }
      console.log(that.data.username)
      console.log(that.data.type1)
      console.log(that.data.date)
      console.log(that.data.type)
      if(app.globalData.role==3){
        var num=0
        wx.request({
          url: app.globalData.url+'/client/query',
          method:'POST',
          header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
                  "token":app.globalData.token,
                },
          data:{
            username:that.data.type3,
            bankArea:that.data.type,
            approvalState:that.data.type1,
            orderTime1:that.data.date,
            orderTime2:that.data.date1
          },
              success(res){
                console.log(res)
                  for(var i=0;i<res.data.data.length;i++){
                    if(res.data.data[i].approvalState==1){
                        res.data.data[i].approvalState='待审核'
                    }
                    if(res.data.data[i].approvalState==2){
                      res.data.data[i].approvalState='待过户'
                    }
                    if(res.data.data[i].approvalState==3){
                      res.data.data[i].approvalState='待抵押'
                    }
                    if(res.data.data[i].approvalState==4){
                      res.data.data[i].approvalState='待放款'
                    }
                    if(res.data.data[i].approvalState==5){
                      res.data.data[i].approvalState='已放款'
                    }
                    if(res.data.data[i].approvalState==6){
                      res.data.data[i].approvalState='审核拒绝'
                    }
                  }
                  for(var i=0;i<res.data.data.length;i++){
                    num+=parseInt(res.data.data[i].loanAmount)
                    console.log(res.data.data[i].loanAmount)
                  }
                that.setData({
                  result:res.data.data,
                  length:res.data.data.length,
                  num:num
                })
              }
      })}

      else if(app.globalData.role==4){
        var num=0
        wx.request({
          url: app.globalData.url+'/client/query2',
          method:'POST',
          header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
                  "token":app.globalData.token,
                },
          data:{
            username:that.data.type3,
            bankArea:that.data.type,
            approvalState:that.data.type1,
            orderTime1:that.data.date,
            orderTime2:that.data.date1
          },
                success(res){
                  console.log(res)
                    for(var i=0;i<res.data.data.length;i++){
                      if(res.data.data[i].approvalState==1){
                          res.data.data[i].approvalState='待审核'
                      }
                      if(res.data.data[i].approvalState==2){
                        res.data.data[i].approvalState='待过户'
                      }
                      if(res.data.data[i].approvalState==3){
                        res.data.data[i].approvalState='待抵押'
                      }
                      if(res.data.data[i].approvalState==4){
                        res.data.data[i].approvalState='待放款'
                      }
                      if(res.data.data[i].approvalState==5){
                        res.data.data[i].approvalState='已放款'
                      }
                      if(res.data.data[i].approvalState==6){
                        res.data.data[i].approvalState='审核拒绝'
                      }
                    }
                    for(var i=0;i<res.data.data.length;i++){
                      num+=parseInt(res.data.data[i].loanAmount)
                      console.log(res.data.data[i].loanAmount)
                    }
                  that.setData({
                    result:res.data.data,
                    length:res.data.data.length,
                    num:num
                  })
                }
        })
      }
    },
    name:function(e){
      var that=this
      let name=e.detail.value
      that.setData({
        username:name
      })
    },
    details:function(){
      var that=this
      if(app.globalData.role==3){
        var num=0
        wx.request({
          url: app.globalData.url+'/client/query',
          method:'POST',
          data:{
          },
          header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
                  "token":app.globalData.token},
          success(res){
              console.log(app.globalData.token)
              console.log(res.data)
              if(res.data.code!=200){
                that.details()
              }
              if(res.data.code==200){
                for(var i=0;i<res.data.data.length;i++){
                  if(res.data.data[i].approvalState==1){
                      res.data.data[i].approvalState='待审核'
                  }
                  if(res.data.data[i].approvalState==2){
                    res.data.data[i].approvalState='待过户'
                  }
                  if(res.data.data[i].approvalState==3){
                    res.data.data[i].approvalState='待抵押'
                  }
                  if(res.data.data[i].approvalState==4){
                    res.data.data[i].approvalState='待放款'
                  }
                  if(res.data.data[i].approvalState==5){
                    res.data.data[i].approvalState='已放款'
                  }
                  if(res.data.data[i].approvalState==6){
                    res.data.data[i].approvalState='审核拒绝'
                  }
                }
                for(var i=0;i<res.data.data.length;i++){
                  num+=parseInt(res.data.data[i].loanAmount)
                  console.log(res.data.data[i].loanAmount)
                }
                console.log(num)
                console.log(res.data.data)
                that.setData({
                  result:res.data.data,
                  length:res.data.data.length,
                  num:num
                })
              }
          }
        })
      }
      else if(app.globalData.role==4){
        var num=0
        wx.request({
          url: app.globalData.url+'/client/query2',
          method:'POST',
          data:{
          },
          header:{"content-type":"application/x-www-form-urlencoded;charset=utf-8",
                  "token":app.globalData.token},
          success(res){
              console.log(app.globalData.token)
              console.log(res.data)
              if(res.data.code!=200){
                that.details()
              }
              if(res.data.code==200){
                for(var i=0;i<res.data.data.length;i++){
                  if(res.data.data[i].approvalState==1){
                      res.data.data[i].approvalState='待审核'
                  }
                  if(res.data.data[i].approvalState==2){
                    res.data.data[i].approvalState='待过户'
                  }
                  if(res.data.data[i].approvalState==3){
                    res.data.data[i].approvalState='待抵押'
                  }
                  if(res.data.data[i].approvalState==4){
                    res.data.data[i].approvalState='待放款'
                  }
                  if(res.data.data[i].approvalState==5){
                    res.data.data[i].approvalState='已放款'
                  }
                  if(res.data.data[i].approvalState==6){
                    res.data.data[i].approvalState='审核拒绝'
                  }
                }
                for(var i=0;i<res.data.data.length;i++){
                  var num=0
                  num+=parseInt(res.data.data[i].loanAmount)
                }
                console.log(res.data.data)
                that.setData({
                  result:res.data.data,
                  length:res.data.data.length,
                  num:num
                })
              }
          }
        })
      }
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    that.details()
    that.salesman()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})